#!/bin/bash
#########################################################
#                                                       #
#              MobileGPS Service Handler                #
#                                                       #
# Written for Pi-Star (http://www.mw0mwz.co.uk/pi-star) #
#               By Andy Taylor (MW0MWZ)                 #
#                                                       #
#                     Version 0.1                       #
#                                                       #
#########################################################

# Service Config
DAEMON=MobileGPS
DAEMON_PATH=/usr/local/bin/
CONFIG=/etc/mobilegps
DAEMON_OPTS=$CONFIG
PGREP=/usr/bin/pgrep
KILL=/bin/kill
SLEEP=/bin/sleep
USER=root
GROUP=mmdvm
LOGDIR=/var/log/pi-star

# Pre-flight checks...
test -x ${DAEMON_PATH}${DAEMON} || exit 0

# if dstarrepeater is configured or running, dont start this daemon!
! test -r /etc/dstar-radio.dstarrepeater || exit 0

# Check if Mobile GPS is enabled in MobileGPS or not
if [[ -f /etc/mobilegps ]]; then
	if [ `sed -n '/\[Enabled\]/{n;p;}' /etc/mobilegps | cut -c 8` == 0 ]; then
		exit 0;
	fi
fi

# Check if we have a config
if [[ ! -f /etc/mobilegps ]]; then
  # Mount the disk RW and download the base config from github
  mount -o remount,rw /
  mount -o remount,rw /boot
  curl --fail -o /etc/mobilegps -s https://raw.githubusercontent.com/g4klx/MobileGPS/master/MobileGPS.ini
  if [[ -f /etc/mobilegps ]]; then
    # Make sure we were able to download the file
    echo -e "" >> /etc/mobilegps
    echo -e "[Enabled]" >> /etc/mobilegps
    echo -e "Enabled=0" >> /etc/mobilegps
  fi
  exit 0;
fi

# Check if the Mobile GPS config is set to enabled also
if [ `sed -n '/\[Enabled\]/{n;p;}' /etc/mobilegps | cut -c 9` != 1 ]; then
	exit 0;
fi

# Verify the logging directory exists, if not create it and setup the ownership / permissions
if [ ! -d $LOGDIR ]; then
  mkdir -p $LOGDIR
  chown ${USER}:${GROUP} $LOGDIR
  chmod 775 $LOGDIR
fi

case "$1" in
	start)
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "$DAEMON is already running as PID "`$PGREP $DAEMON`
			exit 0;
		else
			nice -n -5 ${DAEMON_PATH}${DAEMON} ${DAEMON_OPTS} &
			echo -e "$DAEMON started as PID "`$PGREP $DAEMON`
			exit 0;
		fi
		;;

	stop)
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "Killing $DAEMON PID "`$PGREP $DAEMON`
			$KILL `${PGREP} ${DAEMON}`
			exit 0;
		else
			echo -e "$DAEMON is not running"
			exit 0;
		fi
		;;

	restart)
		if [ `$PGREP $DAEMON` ]; then
			echo -e "Killing $DAEMON PID "`$PGREP $DAEMON`
			$KILL `${PGREP} ${DAEMON}`
			$SLEEP 3
			nice -n -5 ${DAEMON_PATH}${DAEMON} ${DAEMON_OPTS} &
			echo -e "$DAEMON re-started as PID "`${PGREP} ${DAEMON}`
			exit 0;
		else
			echo -e "$DAEMON is not running"
			nice -n -5 ${DAEMON_PATH}${DAEMON} ${DAEMON_OPTS} &
			echo -e "$DAEMON started as PID "`${PGREP} ${DAEMON}`
			exit 0;
		fi
		;;

	status)
		if [ `${PGREP} ${DAEMON}` ]; then
			echo -e "$DAEMON is running as PID "`${PGREP} ${DAEMON}`
		else
			echo -e "$DAEMON is not running"
		fi
		;;

	*)
		echo $"Usage: $0 {start|stop|restart|status}"
		exit 0
esac
